package handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;

public class MessageEncoder extends ChannelOutboundHandlerAdapter
{
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
    {
        ByteBuf buf           = (ByteBuf) msg;
        int     oldWriteIndex = buf.writerIndex();
        int     length        = oldWriteIndex - 4;
        //报文头写入报文体的长度后，恢复正确的写入下标位置。否则在输出的时候数据会出错。
        buf.writerIndex(0).writeInt(length).writerIndex(oldWriteIndex);
        ctx.write(msg, promise);
    }
}
